<?php
class Project_Product_Table extends CF_DbTable_Table{
	protected $_name = 'products';
	protected $_primary = 'product_id';

	public function getProducts($limit = null)
	{
		$select = $this->getAdapter()->select()
			->from(array('pr' => $this->_name),
				array('product_id','model','description','quantity','register_date','price'))
			->joinLeft(array('ph'=>'photos'),
				   'pr.product_id=ph.product_id',
					array('photo'=>'link'))
			->order('pr.product_id DESC');
		if(!is_null($limit))
		{
			$select->limit($limit);
		}
		$resource = $this->getAdapter()->query($select);
		$result = $resource->fetchAll(Zend_Db::FETCH_OBJ);
		//$result = $resource->fetchAll();

		return $result;

	}
	public function getProduct($productId){
		if(isset($productId)){
			$product = $this->fetchRow(
				$this->select()
				->where('product_id=?',$productId)
			);
			return $product;
		}else{
			return false;
		}
	}
	public function addProduct(array $productData)
	{
		if(!empty($register_date)){
			$register_date = date($register_date);
		}else{
			$register_date = date('dd:mm:yyyy');
		}
		$data = array(
            'category_id'  => $productData['category_id'],
            'model'        => $productData['model'],
			'description'  => $productData['description'],
			'price'        => $productData['price'],
			'quantity'     => $productData['quantity'],
			'register_date'=> $productData['register_date']
		);
		$this->insert($data);
	}

	public function updateProduct(array $productData)
	{

		if(!empty($register_date)){
			$register_date = date($register_date);
		}else{
			$register_date = date('dd:mm:yyyy');
		}
		$id = $productData['id'];
		$data = array(
            'category_id'  => $productData['category_id'],
            'model'        => $productData['model'],
			'description'  => $productData['description'],
			'price'        => $productData['price'],
			'quantity'     => $productData['quantity'],
			'register_date'=> $productData['register_date']
		);
		$this->update($data, 'product_id = '. (int)$id);
	}

	public function deleteProduct($id)
	{
		$this->delete('product_id =' . (int)$id);
	}
}